package org.dftproject.genesis.ui.presentation.tabcontrol;

import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;

public class TabButtonFigure extends TabFigure {
	
	public TabButtonFigure(TabControlFigure parent) {
		super(parent);
		
		setCursor(Cursors.HAND);
	}

	@Override
	protected void addListeners() {
		MouseTracker tracker = new MouseTracker();
		addMouseMotionListener(tracker);
		addMouseListener(tracker);
	}

	@Override
	public Dimension getPreferredSize(int wHint, int hHint) {
		Insets insets = getInsets();
		return new Dimension(label.getPreferredSize(wHint, hHint)).expand(insets.getWidth(), insets.getHeight());
	}
	
	protected class MouseTracker extends MouseMotionListener.Stub implements MouseListener {

		private boolean mouseDown;

		public void mousePressed(MouseEvent me) {
			if (me.button == 1) {
				requestFocus();
				me.consume();
	
				mouseDown = true;
			}
		}

		public void mouseReleased(MouseEvent me) {
			if (mouseDown && me.button == 1) {
				if (getBounds().contains(me.getLocation()))
					handleSelected();
	
				mouseDown = false;
			}
		}

		public void mouseDoubleClicked(MouseEvent me) {
		}

		@Override
		public void mouseEntered(MouseEvent me) {
			hover = true;
			repaint();
		}

		@Override
		public void mouseExited(MouseEvent me) {
			hover = false;
			repaint();
		}

		@Override
		public void mouseDragged(MouseEvent me) {
			hover = getBounds().contains(me.getLocation());
			repaint();
		}

	}

}
